package thread;

/*
 * @author zzr
 * @date: 2025/04/03  21:03
 * @description:
 */
public class ThreadDemo37 {
    private static int TIMES = 10;
    private static int state = 0;

    public static void main(String[] args) {
        Object locker = new Object();

        Thread threadA = new Thread(() -> {
            for (int i = 0; i < TIMES; i++) {
                synchronized (locker) {
                    while (state % 3 != 0) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("A");
                    state++;
                    locker.notifyAll();
                }
            }
        });

        Thread threadB = new Thread(() -> {
            for (int i = 0; i < TIMES; i++) {
                synchronized (locker) {
                    while (state % 3 != 1) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("B");
                    state++;
                    locker.notifyAll();
                }
            }
        });

        Thread threadC = new Thread(() -> {
            for (int i = 0; i < TIMES; i++) {
                synchronized (locker) {
                    while (state % 3 != 2) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("C");
                    state++;
                    locker.notifyAll();
                }
            }
        });

        threadA.start();
        threadB.start();
        threadC.start();


    }
}
